package coms.process;


import java.util.ArrayList;
import java.util.Map;
import org.slf4j.LoggerFactory;
import com.beans.AdminUserBean;
import com.beans.CategoryBean;
import com.beans.CityBean;
import com.beans.ClaimBean;
import com.beans.FormBean;
import com.beans.ORGBean;
import com.beans.ProjectBean;
import com.beans.SubCategory;
import com.beans.TravelStandard;
import com.beans.VEHICLE;
import com.crc.mam.BaseCase;
import TestCases.BaseTestCase;
import coms.AutoException;
import frame.FrameWork;
import frame.InterTest;
import functions.ems_base_data.Category;
import functions.ems_base_data.City;
import functions.ems_base_data.MdOu;
import functions.ems_base_data.Project;
import functions.ems_claim.Claim;
import functions.ems_claim.ClaimLine;
import functions.ems_claim.ClaimProcess;
import functions.ems_claim.ClaimTravelInfo;
import functions.ems_claim.Travel;
import functions.ems_process_service.Psc;
import functions.mams_rest.Sys;
import functions.mams_rest.User;

public class TravelClaimProcess extends BaseCase implements InterTest {
  private static org.slf4j.Logger logger = LoggerFactory.getLogger(TravelClaimProcess.class);
	String claimNo;
	String claimId;
	String amount;
	String categoryCode;
	String categoryName;
	ORGBean applyOrgBean;
	String applyCompany;
	
	public ORGBean getApplyOrgBean() {
    return applyOrgBean;
  }

  public void setApplyOrgBean(ORGBean applyOrgBean) {
    this.applyOrgBean = applyOrgBean;
  }

  public String getApplyCompany() {
    return applyCompany;
  }

  public void setApplyCompany(String applyCompany) {
    this.applyCompany = applyCompany;
  }

  public String getCategoryName() {
    return categoryName;
  }

  public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
  }

  public String getCategoryCode() {
    return categoryCode;
  }

  public void setCategoryCode(String categoryCode) {
    this.categoryCode = categoryCode;
  }

  public String getAmount() {
    return amount;
  }

  public void setAmount(String amount) {
    this.amount = amount;
  }

  public String getClaimNo() {
		return claimNo;
	}

	public void setClaimNo(String claimNo) {
		this.claimNo = claimNo;
	}

	public String getClaimId() {
		return claimId;
	}

	public void setClaimId(String claimId) {
		this.claimId = claimId;
	}



	/**
	 * 由于不同环境的业务大类code不同，所以必须填其一，categoryCode或者categoryName
	 */
	@Override
	public void run(Map<Object, Object> paramMap) {
	  logger.info("差旅事前申请报账单------start-----");
	  String baseUrl = (String) paramMap.get("baseUrl");
      if(baseUrl !=null) {
        FrameWork.setBaseUrl(baseUrl);
      }
      LoginTest loginTest = new LoginTest();
      loginTest.run(paramMap);
      if(!loginTest.status) {
        remark = loginTest.result;
        status = false;
        throw new AutoException("登录失败: "+ remark +"，阻碍测试");
      }
	  /**
       * 
       * 事前费用申请
       */
      Psc psc = new Psc();
      CategoryBean categoryBean = null ;
      categoryCode = (String) paramMap.get("categoryCode");
      categoryName = (String) paramMap.get("categoryName");
      if(categoryCode == null) {
        Category category = new Category();
        categoryBean = category.getCategoryByName(categoryName);
        categoryCode = categoryBean.getCategoryCode();
        if(categoryBean == null) {
          throw new AutoException("找不到对应的业务大类：" +categoryName+ ", 请确认后重新提交");
        }
      }
      logger.info("categoryCode: " + categoryCode+", categoryName: " + categoryName+"---创建表单");
      psc.selectFlowParam(categoryCode);
    
      FormBean formBean = psc.getForm();
      /*LoginTest login = new LoginTest();*/
      Claim claim = new Claim(categoryCode,categoryName);
      AdminUserBean bean = claim.findCurrentEmployee();
      claim.setLoginUserBean(bean);
     // logger.info("提交表单员工信息："+bean.toString());
      claim.saveTravelClaimHeadAndLine();
      if(!claim.status) {
        status = false;
        remark = claim.getRemark();
        throw new AutoException("表单头保存失败"+remark+",无法继续测试");
      }
     
      claim.getClaimHeadAndLine();
      ClaimBean responseClaimBean = claim.getResponseClaimBean();
      claimId = responseClaimBean.getClaimId();
      claimNo = responseClaimBean.getClaimNo();
      logger.info("保存表单头，并得到claimId: " + claimId);

      MdOu ou = new MdOu(bean);
      ou.getCompanyByCompanyName(bean.getCompanyDesc());
      String compayName = ou.getMyCompany().getCompanyName();
      
      City city = new City();
      city.findCityListForTrip();
      CityBean startCity = city.getStartCity();
      CityBean desCity = city.getDesCity();
      Sys sys = new Sys();
      sys.vehicle();
      VEHICLE vehicle = sys.getSelectVehicle();
      String areaCode = desCity.getCityType();
      String companyCode = ou.getMyCompany().getCompanycode();
      String empId = bean.getEmpNo();
      String orgCode = bean.getDepartmentId();
      Travel travel = new Travel();
      travel.findTravelStandard(areaCode,companyCode,empId,orgCode);
      TravelStandard standard = travel.getTravel();
      Project project = new Project();
      project.findPagedProjectList(companyCode);
      ProjectBean selectProject = project.getSelectProject();
      sys.vehicleLevel();
      VEHICLE selectLevel = sys.getSelectLevel();
      //
      // 差旅费事前申请
      ClaimTravelInfo info = new ClaimTravelInfo();
      Category category = new Category();
      category.subcategory(categoryCode);
      ArrayList<SubCategory> subCategories = category.getSubCategories();
      SubCategory subCategory = subCategories.get(0);
      info.setSubcategoryCode(subCategory.getSubCategoryCode());
      info.setSubCategoryName(subCategory.getSubCategoryName());
      info.setResponseBean(responseClaimBean);
      info.setDesCity(desCity);
      info.setStartCity(startCity);
      info.setSelectVehicle(vehicle);
      info.setSelectLevel(selectLevel);
      info.setSelectProject(selectProject);
      info.setTravel(standard);       
      info.insert(false);
      if(!info.status){
        status = false;
        remark = info.getResult();
        throw new AutoException("插入报账明细失败，无法继续进行测试");
      }
      applyOrgBean = info.getApplyOrgOrg();
      applyCompany = info.getApplyCompany();
      amount = info.getAmount();
      ClaimLine line = new ClaimLine();
      line.findTravelInfoList(claimId);
      ClaimProcess process = new ClaimProcess();
      process.submitProcess(claimId,formBean);
     
      if(!process.status) {
        status = false;
        remark = process.remark;
       
      }else {
     
        status = true;
        remark = "[claimId="+claimId+",claimNum="+claimNo+",amount="+amount+",applyOrgBean="+applyOrgBean.toString()+",applyCompany="+applyCompany.toString()+"]";
      }
	}

  @Override
  public boolean getStatus() {
    // TODO Auto-generated method stub
    return status;
  }

  @Override
  public String getRemark() {
    // TODO Auto-generated method stub
    return remark;
  }
}
